package me.sdevil507.biz.fruit.controller;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.biz.fruit.dto.FruitDTO;
import me.sdevil507.biz.fruit.dto.FruitParamsDTO;
import me.sdevil507.biz.fruit.mapper.FruitMapper;
import me.sdevil507.biz.fruit.service.FruitImportExportService;
import me.sdevil507.biz.fruit.service.FruitService;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.shiro.helper.OrgAccountHelper;
import me.sdevil507.supports.status.ApiStatusCode;
import me.sdevil507.supports.status.ComConstant;
import me.sdevil507.systemAssist.Constant.SystemAssistConstant;
import me.sdevil507.systemAssist.systemLog.annotation.OperLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @program: tips_back_end
 * @description: 成果展示类
 * @author: kzl
 * @create: 2020-06-23 16:39
 **/
@RestController
@RequestMapping("/api/expert/fruit")
@Slf4j
public class FruitExeController {

    @Autowired
    private FruitService fruitService;

    /**
     * 获取专家主页的百分比数据
     * @return
     */
    @PostMapping("/getExpertHomePercentData")
    public ApiResultDTO getExpertHomePercentData(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDTO;
        //
        try {
            fruitParamsDTO.getSearchDto().setOrgUserId(OrgAccountHelper.getUserId());
            HashMap<String,Object> resultMap = fruitService.getExpertHomePercentData(fruitParamsDTO.getSearchDto());
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), resultMap);
        } catch (Exception e) {
            //事务回滚
            log.error("获取成果百分比数据失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果百分比数据失败");
        }
        return apiResultDTO;
    }

    /**
     * 获取专家主页的折线数据（成果和批示）
     * @return
     */
    @PostMapping("/getExpertHomeLineData")
    public ApiResultDTO getExpertHomeLineData(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDTO;
        //
        try {
            fruitParamsDTO.getSearchDto().setOrgUserId(OrgAccountHelper.getUserId());
            HashMap<String,Object> resultMap = fruitService.getExpertHomeLineData(fruitParamsDTO.getSearchDto());
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), resultMap);
        } catch (Exception e) {
            //事务回滚
            log.error("获取成果百分比数据失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果百分比数据失败");
        }
        return apiResultDTO;
    }


    /**
     * @ClassName FruitExeController
     * @Description : a.获取专家的机构数与对应成果的列表:
     *   ----结果：（机构名称、机构logo、当前专家在本机构的成果数）
     *   ----逻辑：查询该专家的所有成果，进行机构分组，然后获取机构和成果数
     *  b.合作过的专家列表
     *   ----结果：专家头像、名称、就职单位
     *   ----逻辑：查询该专家的所有成果，进行专家分组，然后查询专家的详细信息
     *
     * @Return : me.sdevil507.supports.result.ApiResultDTO
     * @Author : kzl
     * @Date : 2020/9/3 22:39
     */
    @PostMapping("/relation")
    public ApiResultDTO relation(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDTO;
        //
        try {
            fruitParamsDTO.getSearchDto().setOrgUserId(OrgAccountHelper.getUserId());
//            HashMap<String,Object> resultMap = fruitService.countByOrgID(fruitDTO);
            HashMap<String,Object> resultMap = fruitService.getExpertRelationData(fruitParamsDTO.getSearchDto());
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), resultMap);
        } catch (Exception e) {
            //事务回滚
            log.error("获取关联专家和机构信息失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取关联专家和机构信息失败");
        }
        return apiResultDTO;
    }

}
